
package finalproject.client;

import java.util.List;



import finalproject.shared.Docent;
import finalproject.shared.Vak;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.HTMLTable.Cell;



public class VakGui {
   
    private static final String LISTING_ROOT_PANEL = "guiListing";
    private static final String FORM_ROOT_PANEL = "guiForm";
    private static final String TOOL_BAR_ROOT_PANEL = "guiToolBar";
    private static final int EDIT_LINK = 2;
    private static final int REMOVE_LINK = 3;

    //buttons en  andere benodigdheden
    
    protected Button addButton;
    protected Button updateButton;
    protected Button addNewButton;
    protected ListBox docentNrField;
    protected TextBox naamField;
    protected Grid listGrid;
    protected Grid formGrid;
   
    
    
    private List<Docent> docenten;
    private List<Vak> vakken;
    private Vak currentVak;
    protected VakClass vakService;
        
  
    public void go() {
    	
        addButton = new Button("Add vak");
        addNewButton = new Button("Add new vak");
        updateButton = new Button("Update vak");
        docentNrField = new ListBox();
        naamField     = new  TextBox();      
       
        listGrid = new Grid(2,5);
       

        buildForm();
        placeWidgets();
    }
 //events aanroepen
   
    public void wireEvents() {
    	listGrid.addClickHandler(new ClickHandler(){
            @Override
			public void onClick(ClickEvent event) {
                 Cell cellForEvent = listGrid.getCellForEvent(event);
                 gui_eventListGridClicked(cellForEvent);                
            }});
        
    	
    	
    	
    	addButton.addClickHandler(new ClickHandler(){
            @Override
			public void onClick(ClickEvent event) {
            	gui_eventAddButtonClicked();
            }});

    	updateButton.addClickHandler(new ClickHandler(){
            @Override
			public void onClick(ClickEvent event) {
            	gui_eventUpdateButtonClicked();
            }});
        
    	addNewButton.addClickHandler(new ClickHandler(){
            @Override
			public void onClick(ClickEvent event) {
            	gui_eventAddNewButtonClicked();
            }});

   
   	
    }
    
    
    private void buildForm() {
        formGrid = new Grid(5,3);
        formGrid.setVisible(false);
        
        formGrid.setWidget(0, 0, new Label("Docent"));
        formGrid.setWidget(0, 1, docentNrField);

        formGrid.setWidget(1, 0, new Label("Naam"));
        formGrid.setWidget(1, 1, naamField);

        
        formGrid.setWidget(2, 0, updateButton);
        formGrid.setWidget(2, 1, addButton);
        
    }

 //widgets in rootpanels zetten
    private void placeWidgets() {
        RootPanel.get(LISTING_ROOT_PANEL).add(listGrid);
        RootPanel.get(FORM_ROOT_PANEL).add(formGrid);
        RootPanel.get(TOOL_BAR_ROOT_PANEL).add(addNewButton);
       
    }

    
    public void hideGui() {
    	formGrid.setVisible(false);
    	listGrid.setVisible(false);
      addNewButton.setVisible(false);

    }

    
    
    private void loadForm(Vak vak) {
 	
        this.formGrid.setVisible(true);
        currentVak = vak;
        //this.docentNrField.setText(vak.getDocentNr().toString());
        this.docentNrField.clear();
        int index = 0;
        int selected = 0;
        for (Docent docent : docenten) {
        	this.docentNrField.addItem(docent.getPersoon().getNaam());
        	if (vak.getDocentNr().longValue() == 
        			docent.getDocentNr().longValue()) {
        		selected = index;
        		//this.docentNrField.setSelectedIndex(index);
        	}
        	index++;
        }
        this.docentNrField.setSelectedIndex(selected);
        this.naamField.setText(vak.getNaam());
    }


  
    private void copyFieldDateToVak() {
    	int selected = this.docentNrField.getSelectedIndex();

    	Long docentNr = 0L;
        if (docenten.size() > selected) {
        	docentNr = docenten.get(selected).getDocentNr();
        }
        
       
        currentVak.setDocentNr(docentNr);
        currentVak.setNaam(naamField.getText());}
      
     
        
    public void gui_eventListGridClicked(Cell cellClicked) {
         int row = cellClicked.getRowIndex();
         int col = cellClicked.getCellIndex();
        
         Vak vak = this.vakken.get(row);
        
         if (col==EDIT_LINK) {
             this.addNewButton.setVisible(false);
             this.updateButton.setVisible(true);
             this.addButton.setVisible(false);
            

             loadForm(vak);
         } else if (col==REMOVE_LINK) {
        	 if (Window.confirm("Verwijderen vak ?")) {
        		 this.vakService.removeVak(vak);
        	 }
         }
    }


    
    public void gui_eventAddButtonClicked() {
        addNewButton.setVisible(true);
        formGrid.setVisible(false);
        copyFieldDateToVak();
        this.vakService.addVak(currentVak);
    }




    public void gui_eventUpdateButtonClicked() {
        addNewButton.setVisible(true);
        formGrid.setVisible(false);
        copyFieldDateToVak();
        this.vakService.updateVak(currentVak);
    }

  
    public void gui_eventAddNewButtonClicked() {
        this.addNewButton.setVisible(false);
        this.updateButton.setVisible(false);
        this.addButton.setVisible(true);
        Vak vak = new Vak();
        vak.setDocentNr(0L);
        
        
        loadForm(vak);
    }
    
    
    public void service_docentenListRetrievedFromService(List<Docent> result) {
    	this.docenten = result;	
    }
    	
    public void service_eventListRetrievedFromService(List<Vak> result) {
    	

        //formGrid.setVisible(false);
    	listGrid.setVisible(true);
    	addNewButton.setVisible(true);
        
        this.vakken = result;
        this.listGrid.clear();
        this.listGrid.resizeRows(this.vakken.size());
        int row = 0;
        this.addNewButton.setVisible(true);
        
        for (Vak vak : result) {
            this.listGrid.setWidget(row, 0, new Label (vak.getNaam()));
            this.listGrid.setWidget(row, EDIT_LINK, new Anchor("Edit"));
            this.listGrid.setWidget(row, REMOVE_LINK, new Anchor("Remove"));
            row ++;
        }
    }

    public void service_eventAddVakSuccessful() {
        this.vakService.listVakken();
    }
    
    public void service_eventUpdateSuccessful() {
        this.vakService.listVakken();
    }
    
   
    public void service_eventRemoveVakSuccessful() {
        this.vakService.listVakken();
    }


    public void service_eventUpdateVakFailed(Throwable caught) {
        Window.alert("Update vak failed");
    }

   
    public void service_eventAddVakFailed(Throwable caught) {
    	 Window.alert("Unable to update vak");
    }

   
    public void service_eventRemoveVakFailed(Throwable caught) {
    	 Window.alert("Remove vak failed");
    }


    public void service_eventListVakkenFailed(Throwable caught) {
    	 Window.alert("Unable to get vak list");
    }

    
}
